<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--<%@ include file="../include/admincontent.jsp" %>--%>
<link rel="stylesheet" href="../resources/js/plugins/layui/css/layui.css"/>
<link rel="stylesheet" href="../resources/css/knowlegPoints.css"/>
<link rel="stylesheet" href="../resources/css/index.css"/>

<%--<div id="loader"><img alt="" src="/Content/Theme/green/image/1d_2.gif"/></div>--%>
<div style="height:100%;">
<div id="location">当前位置：首页 > 试卷设计 > 专业试卷管理 > 试卷题目管理</div>
<div id="Content">
    <div style="display: none">
        <input name="majorInput" value="${major}">
        <input name="pidInput" value="${pid}">
    </div>
    <form id="topicForm" >
        <div class="btnbar">
            <button type="button" class="save" id="btn_topic_save">保 存</button>
            <button type="button" class="back" onclick="self.location=document.referrer;">返 回</button>
            <div class="infotitle">试卷名称：${pname}</div>
        </div>

        <c:if test="${!empty titleType0}">
            <table id="titleType0" class="treeTable inputform" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom:10px;">
                <tbody>
                <tr class="parttitle">
                    <td width="100">题型：</td>
                    <td>${titleType0}</td>
                </tr>
                <tr>
                    <td>说明：</td>
                    <td><textarea name="remark0"></textarea></td>
                </tr>
                <tr>
                    <td rowspan=${count0}>题目：</td>
                    <td  class="q">
                        <a rel=${tnum0}>${tContent0}</a>
                    </td>
                </tr>
                <c:forEach items="${sChoose}" var="item">
                    <tr>
                        <td  class="q">
                            <a rel=${item.tN}>${item.tContent}</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:if>

        <c:if test="${!empty titleType1}">
            <table id="titleType1" class="treeTable inputform" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom:10px;">
                <tbody>
                <tr class="parttitle">
                    <td width="100">题型：</td>
                    <td>${titleType1}</td>
                </tr>
                <tr>
                    <td>说明：</td>
                    <td><textarea name="remark1"></textarea></td>
                </tr>
                <tr>
                    <td rowspan=${count1}>题目：</td>
                    <td  class="q">
                        <a rel=${tnum1}>${tContent1}</a>
                    </td>
                </tr>
                <c:forEach items="${mChoose}" var="item">
                    <tr>
                        <td  class="q">
                            <a rel=${item.tN}>${item.tContent}</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:if>

        <c:if test="${!empty titleType2}">
            <table id="titleType2" class="treeTable inputform" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom:10px;">
                <tbody>
                <tr class="parttitle">
                    <td width="100">题型：</td>
                    <td>${titleType2}</td>
                </tr>
                <tr>
                    <td>说明：</td>
                    <td><textarea name="remark2"></textarea></td>
                </tr>
                <tr>
                    <td rowspan=${count2}>题目：</td>
                    <td  class="q">
                        <a rel=${tnum2}>${tContent2}</a>
                    </td>
                </tr>
                <c:forEach items="${completion}" var="item">
                    <tr>
                        <td  class="q">
                            <a rel=${item.tN}>${item.tContent}</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:if>

        <c:if test="${!empty titleType3}">
            <table id="titleType3" class="treeTable inputform" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom:10px;">
                <tbody>
                <tr class="parttitle">
                    <td width="100">题型：</td>
                    <td>${titleType3}</td>
                </tr>
                <tr>
                    <td>说明：</td>
                    <td><textarea name="remark3"></textarea></td>
                </tr>
                <tr>
                    <td rowspan=${count3}>题目：</td>
                    <td  class="q">
                        <a rel=${tnum3}>${tContent3}</a>
                    </td>
                </tr>
                <c:forEach items="${trues}" var="item">
                    <tr>
                        <td  class="q">
                            <a rel=${item.tN}>${item.tContent}</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:if>

        <c:if test="${!empty titleType4}">
            <table id="titleType4" class="treeTable inputform" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom:10px;">
                <tbody>
                <tr class="parttitle">
                    <td width="100">题型：</td>
                    <td>${titleType4}</td>
                </tr>
                <tr>
                    <td>说明：</td>
                    <td><textarea name="remark4"></textarea></td>
                </tr>
                <tr>
                    <td rowspan=${count4}>题目：</td>
                    <td  class="q">
                        <a rel=${tnum4}>${tContent4}</a>
                    </td>
                </tr>
                <c:forEach items="${simples}" var="item">
                    <tr>
                        <td  class="q">
                            <a rel=${item.tN}>${item.tContent}</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:if>

        <c:if test="${!empty titleType5}">
            <table id="titleType5" class="treeTable inputform" cellpadding="0" cellspacing="0" width="100%" style="margin-bottom:10px;">
                <tbody>
                <tr class="parttitle">
                    <td width="100">题型：</td>
                    <td>${titleType5}</td>
                </tr>
                <tr>
                    <td>说明：</td>
                    <td><textarea name="remark5"></textarea></td>
                </tr>
                <tr>
                    <td rowspan=${count5}>题目：</td>
                    <td  class="q">
                        <a rel=${tnum5}>${tContent5}</a>
                    </td>
                </tr>
                <c:forEach items="${summary}" var="item">
                    <tr>
                        <td  class="q">
                            <a rel=${item.tN}>${item.tContent}</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </c:if>
    </form>
</div>
</div>
<div style="display: none">
    <table id="cacheTable" lay-filter="cacheTable" class="treeTable" width="100%" cellpadding="0" cellspacing="0" ></table>
</div>

<script type="text/javascript" src="../resources/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../resources/js/plugins/layui/layui.js"></script>
<script type="text/javascript" src="../resources/js/actions.js"></script>
<script src="../resources/js/fadeInout.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">
   setWidth();
   var getClickRel;
   var getClickID;
   var getClickNum;
   var getClickTableNum;
   
   var tKonwledge;
   var tLevel;
   var tkey;

   var changeFlag = false;
   var changeList = [];
   var changeNum = 0;

$(function () {
    var major = $("input[name='majorInput']").val();
    var pid = $("input[name='pidInput']").val();


    $(document).delegate('.inputform .q a', 'click', function () {
        getClickRel = $(this).attr('rel');
        getClickID = $(this).closest("table").attr("id");
        getClickNum = $(".inputform .q a").index(this);
        var getClickNum0 = $("#titleType0 .q a").index(this);
        var getClickNum1 = $("#titleType1 .q a").index(this);
        var getClickNum2 = $("#titleType2 .q a").index(this);
        var getClickNum3 = $("#titleType3 .q a").index(this);
        var getClickNum4 = $("#titleType4 .q a").index(this);
        var getClickNum5 = $("#titleType5 .q a").index(this);

        for(var m=0;m<6;m++ ){
            if(getClickNum0 >=0 ){
                getClickTableNum = getClickNum0;
                break;
            }else if(getClickNum1 >=0 ){
                getClickTableNum = getClickNum1;
                break;
            }else if(getClickNum2 >=0 ){
                getClickTableNum = getClickNum2;
                break;
            }else if(getClickNum3 >=0 ){
                getClickTableNum = getClickNum3;
                break;
            }else if(getClickNum4 >=0 ){
                getClickTableNum = getClickNum4;
                break;
            }else if(getClickNum5 >=0 ){
                getClickTableNum = getClickNum5;
                break;
            }
        }

       layui.use(['table', 'form'], function () {
            layer.open({
                type: 2,
                title: '修改试卷题目',
                shade: 0.5,
                offset: '30px',
                area: ['800px', '600px'],
                content: encodeURI('/PaperManager/choose?' + getClickRel + "&major=" + major),
                end: function(){
                    var getDateCache = layui.table.cache.cacheTable;
                    for(var i = 0;i< getDateCache.length; i++){
                        if((getDateCache[i].changed === true)&&(getClickNum === getDateCache[i].idnum)){
                            changeFlag = true;
                            var mytable = document.getElementById(getClickID);
                            if(getClickTableNum === 0)
                                mytable.children[0].children[2].children[1].outerHTML =
                                    '<td  class="q"><a rel='+getDateCache[i].rel+'>'+getDateCache[i].name+'</a></td>';
                            else
                                mytable.children[0].children[getClickTableNum+2].children[0].outerHTML =
                                    '<td  class="q"><a rel='+getDateCache[i].rel+'>'+getDateCache[i].name+'</a></td>';
                        }
                    }
                }

            })
        });
    });

    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#cacheTable',
            url: '/PaperManager/getSelectQuesLayui?pid=' + pid + '&major=' + major, //数据接口
            page: true,
            cols: [[
                {field: 'name', title: '题目名称',},
            ],],
            done: function (res, curr, count) {
                var getdat = layui.table.cache['cacheTable'];

                var tdlist = $(".inputform .q a");
               for(var i=0;i<tdlist.length;i++){
                   for(var j=0;j<getdat.length;j++){
                       if(tdlist[i].rel === getdat[j].rel){
                           getdat[j].idnum = i;
                           break;
                       }
                   }
               }
                layui.table.cache['cacheTable'] = getdat;

                for(var i=0;i<tdlist.length;i++){
                    if(tdlist[i].innerHTML.indexOf("可能由于题目被删除等情况，")>=0){
                        tdlist[i].style="color:red";
                    }
                }
            }
        });
    });

   //更改题目
   $("#btn_topic_save").on('click', (e) => {
       if(!changeFlag){
           layui.use('layer', function () {
                layer.msg('未选择更改题目！',{icon:2});
           });
       } else {
           var idTemp = GetQueryString('id');
           var pnameTemp = GetQueryString('pname');

           var params = {id:idTemp,pname:pnameTemp,chdata:JSON.stringify(layui.table.cache['cacheTable'])};

           $.ajax({
               url:'/PaperManager/saveQuestion',
               data:params,
               dataType:'JSON',
               type:'POST',
               success:function (data) {
                   if (data.state){
                       changeFlag = false;

                       var getdat = layui.table.cache['cacheTable'];

                       var tdlist = $(".inputform .q a");
                       for(var i=0;i<tdlist.length;i++){
                           for(var j=0;j<getdat.length;j++){
                               if(tdlist[i].rel === getdat[j].rel){
                                   getdat[j].changed = false;
                                   break;
                               }
                           }
                       }
                       layui.table.cache['cacheTable'] = getdat;

                       layer.msg(data.Message, {icon: 1, time: 2000,offset:30});
                   }
                   else{
                       layer.msg(data.Message, {icon: 2, time: 2000,offset:30});
                   }
               }
           });
       }
   });

})
</script>